package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {

    @Override
    public ResponseResult findByNameAndPage(ChannelDTO dto) {
        //数据校验
        if (dto == null) {
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页参数校验
        dto.checkParam();
        // 按名称模糊扉页查询
        Page resPage = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();
        //校验 如果名字存在就进行模糊查询
        if (StringUtils.isNotBlank(dto.getName())) {
            wrapper.like(AdChannel::getName, dto.getName());
        }
        //判断状态状态
        if (dto.getStatus()!=null){
            wrapper.eq(AdChannel::getStatus,dto.getStatus());
        }
        // 添加排序方式
        wrapper.orderByAsc(AdChannel::getOrd);

        // 实现分页
        IPage iPage = this.page(resPage, wrapper);

        return new PageResponseResult(dto.getPage(), dto.getSize(), iPage.getTotal(), iPage.getRecords());
    }
    //新增频道

    @Override
    public ResponseResult insert(AdChannel channel) {
        // 判断名称补位空
        String name = channel.getName();
        if (StringUtils.isBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道以存在");
        }
        if (name.length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称过长");
        }
        // 频道名称是否重复
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道名已经存在");

        }
        channel.setCreatedTime(new Date());
        save(channel);

        return ResponseResult.okResult();
    }

    //修改操作

    @Override
    public ResponseResult update(AdChannel adChannel) {
        Integer id = adChannel.getId();
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        AdChannel newChannel = this.getById(id);
        if (newChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST, "id不存在");
        }
        if (adChannel.getName().length() > 10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称过长");
        }

        // 检验名称唯一
        if (StringUtils.isNotBlank(adChannel.getName()) && !adChannel.getName().equals(newChannel.getName())) {
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(
                    AdChannel::getName, adChannel.getName()
            ));
            if (count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道已经存在");
            }
        }
        if ("".equals(adChannel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道已经存在");
        }
        this.updateById(adChannel);
        //4 返回结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {

        //1.检查参数
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        AdChannel channel = this.getById(id);
        // 正常查询是否存在
        if(channel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //启动状态下不能删除
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW);
        }

        this.removeById(id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
